/*
 *    Copyright (c) 2014 Xiang Xu <xuxiang@mail.bnu.edu.cn>
 *
 *    Permission is hereby granted, free of charge, to any person
 *    obtaining a copy of this software and associated documentation
 *    files (the "Software"), to deal in the Software without
 *    restriction, including without limitation the rights to use,
 *    copy, modify, merge, publish, distribute, sublicense, and/or sell
 *    copies of the Software, and to permit persons to whom the
 *    Software is furnished to do so, subject to the following
 *    conditions:
 *
 *    The above copyright notice and this permission notice shall be
 *    included in all copies or substantial portions of the Software.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *    OTHER DEALINGS IN THE SOFTWARE.
 */

#include "arguments.h"
#include "facade_grammar.h"
#include "facade_model.h"

#include "pcl/io/pcd_io.h"
#include "opencv2/opencv.hpp"
#include "boost/program_options.hpp"
#include "boost/timer.hpp"
#include "boost/smart_ptr.hpp"

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <assert.h>

using namespace std;

boost::shared_ptr<FacadeGrammar> facade_grammar;
boost::shared_ptr<FacadeModel> facade_model;

// Main function
int main(int argc, char** argv)
{
    Status status;
	 
    // parse parameters
    boost::shared_ptr<Arguments> arguments = boost::shared_ptr<Arguments>(new Arguments);
    status = arguments->Parse(argc, argv);

    if(!status.IsOk())
    {
        cout << status;
        exit(-1);
    }
    else
    {
        cout << "Argments Parsed success.\n";
    }

    // fetch parameters
    int episodes = arguments->GetLearningEpisodes();
    int g_episodes = arguments->GetGreedyLearningEpisodes();
    double learning_rate = arguments->GetLearningRate();;
    bool state_group = arguments->StateGroup();
    std::vector<double>& weights = arguments->GetSymbolWeights();
    std::string& qtable_file = arguments->GetQFile();

    // Step 1
    cout << "\nLoad the shape grammar.\n";
    facade_grammar = boost::shared_ptr<FacadeGrammar>(new FacadeGrammar);

    status = facade_grammar->ReadGrammar(arguments->GetGrammarFile());
    if (!status.IsOk())
	 {
        cout << status;
        exit(-1);
    }

    // Step 2
    cout << "\nInitialize the facade model, ";
    facade_model = boost::shared_ptr<FacadeModel>(new FacadeModel(facade_grammar));

    // Step 3
    cout << "\nLoad the facade grid ...\n";
    facade_model->LoadGrids(arguments->GetGridFile());

    // Step 4
    std::cout << "\nRunning algorithm ... \n";
    facade_model->set_action_parameter_file(arguments->GetActionParameterFile());
    facade_model->InitModel(state_group);
    facade_model->set_qtable_path(qtable_file);
    facade_model->SetSymbolWeights(weights);

    if(arguments->LoadQTable())
    {
        facade_model->LoadQTable();
    }

    boost::timer timer;
    double score = facade_model->RunParsingAlgorithm(episodes, g_episodes, learning_rate);
    std::cout << "Time elapsed: " << timer.elapsed() << std::endl;

    // Step 5
    std::cout << "\nRendering result ...\n";
    facade_model->RenderResult(arguments->GetResultImageFile());
    facade_model->SaveLearningResult(arguments->GetResultAsciiFile(), score);
	 std::cout << "\nDone\n";
    return 0;
}

